package com.xhx.inputbox;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xhx.inputbox.ChatInputView;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private ChatInputView chatInputView;

    private RecyclerView rvMessages;
    private MessageAdapter messageAdapter;
    private List<MessageItem> messageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        chatInputView = findViewById(R.id.chat_input_view);
        rvMessages = findViewById(R.id.rv_messages);
        initMessageList();
        // 设置监听器来接收来自组件的事件
        chatInputView.setOnInputListener(new ChatInputView.OnInputListener() {
            @Override
            public void onSendMessage(String message) {
                // 处理发送消息的逻辑，例如添加到 RecyclerView
                Toast.makeText(ChatActivity.this, "发送: " + message, Toast.LENGTH_SHORT).show();

                if (TextUtils.isEmpty(message.toString().trim())) {
                    Toast.makeText(ChatActivity.this, "不能发送空消息", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 创建一个新的 MessageItem
                MessageItem sentMessage = new MessageItem(message, MessageItem.MessageType.SENT);

                // 添加到适配器并滚动到底部
                messageAdapter.addMessage(sentMessage);
                rvMessages.scrollToPosition(messageAdapter.getItemCount() - 1);

                // (可选) 模拟接收一条消息
                simulateReceiveMessage();


            }

            @Override
            public void onVoiceClick() {
                // 处理点击语音按钮的逻辑
                Toast.makeText(ChatActivity.this, "语音按钮点击", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMoreOptionClick(String optionName) {
                Toast.makeText(ChatActivity.this, optionName+"按钮点击", Toast.LENGTH_SHORT).show();

            }
            @Override
            public void onEmojiClick(String emojiTag) {
                chatInputView.insertText(emojiTag);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        chatInputView.hideAllPanels();
    }

    // 重写返回键逻辑，优先关闭面板
    @Override
    public void onBackPressed() {
        if (chatInputView.onInterceptBackPressed()) {
            return;
        }
        super.onBackPressed();
    }


    private void initMessageList() {
        messageList = new ArrayList<>();
        messageAdapter = new MessageAdapter(messageList);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rvMessages.setLayoutManager(layoutManager);
        rvMessages.setAdapter(messageAdapter);

        // (可选) 添加一条初始的欢迎消息
        messageList.add(new MessageItem("你好！开始聊天吧。", MessageItem.MessageType.RECEIVED));
        messageAdapter.notifyDataSetChanged();
    }

    // 模拟接收消息，用于演示
    private void simulateReceiveMessage() {
        // 延迟 1 秒后执行
        rvMessages.postDelayed(() -> {
            MessageItem receivedMessage = new MessageItem("我收到了你的消息！", MessageItem.MessageType.RECEIVED);
            messageAdapter.addMessage(receivedMessage);
            rvMessages.scrollToPosition(messageAdapter.getItemCount() - 1);
        }, 1000);
    }
}